import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:znmm/UI/A_Common/animation_dialog.dart';
import 'package:znmm/UI/CategoryPages/mag/category_mag.dart';
import 'package:znmm/UI/CategoryPages/view/category_left_view.dart';
import 'package:znmm/UI/CategoryPages/view/category_list_view.dart';
import 'package:znmm/UI/ProductPages/show_product_view.dart';
import 'package:znmm/UI/utils/my_global.dart';
import 'package:znmm/config/app_color.dart';
import 'package:znmm/network/notify_listener.dart';

import '../model/product_model.dart';

class ProductCategoryView extends StatefulWidget {
  const ProductCategoryView({super.key});

  @override
  State<ProductCategoryView> createState() => _ProductCategoryViewState();
}

class _ProductCategoryViewState extends State<ProductCategoryView> {
  late List<ProductModel> _pModelList = [];
  int _iNotiId1 = -1;

  @override
  void initState() {
    super.initState();

    CategoryMag().reqProductByCateSlugGraql(CategoryMag().firstSlug);

    _pModelList = CategoryMag().getCategProducList();
    //添加监听者
    _iNotiId1 = NotifyCenter().addObserver(NotifyKey.categProductKey, (object) {
      //print("---刷新---");
      if (mounted) {
        setState(() {
          _pModelList = CategoryMag().getCategProducList();
        });
      }
    });
  }

  @override
  void dispose() {
    NotifyCenter().removeCallback(_iNotiId1);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: AppColor.backgroundColor,
        appBar: MyGlobal.getNavigationBar(
            context, "Category / ${CategoryMag().categorySlug}",
            actions: [
              IconButton(
                onPressed: () {
                  showAnimationDialog(
                    context: context,
                    builder: (context) {
                      return CategoryLeftView(
                        onSelCallBack: () {
                          if (mounted) {
                            setState(() {});
                          }
                        },
                      );
                    },
                    transitionType: TransitionType.inFromLeft,
                    barrierDismissible: true,
                  );
                },
                icon: const Icon(Icons.list),
                iconSize: 25,
                color: Colors.black,
              )
            ]),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              margin: const EdgeInsets.fromLTRB(16, 5, 10, 5),
              child: Text(
                "Products: ${_pModelList.length}",
                overflow: TextOverflow.ellipsis,
                style:
                    const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
              ),
            ),
            Expanded(
                child: Container(
              color: Colors.white,
              padding: const EdgeInsets.all(5),
              child: _pModelList.isNotEmpty
                  ? CategoryListView(
                      prodcList: _pModelList,
                      onTap: (pModel) {
                        String sulgStr = pModel.slug;
                        print("sulgStr:$sulgStr");
                        CategoryMag().reqProductBySlugGraql(
                          sulgStr,
                          callback: (p0) {
                            ProductModel pmodel = p0;
                            Get.to(ShowProductView(
                              product: pmodel,
                            ));
                          },
                        );
                      },
                    )
                  : MyGlobal.emptyWidget(),
            ))
          ],
        ));
  }
}
